Ext.namespace("Task.FeedBack");

Task.FeedBack.win = function() {
	this.users = [];
	this.origin = '1';//来源
	this.userSelector = null;
	this.selectUserId = [];
	this.loadMask = null;
	this.taskId;
	this.taskExeId;
	this.taskExeStatus;
	this.taskSender;
	this.taskExecutor;
	this.to;
	this.userSelector2 = null;
	this.selectUserId2 = [];
	this.txtTitle = new Ext.form.TextField({
        fieldLabel: '任务名称',
        width: 300,
        readOnly:true,
        blankText: '请输入任务名称!'
    });
	this.txtExecutor = new Ext.form.TextField({
		fieldLabel: '执行人',
		disabled:true,
		width: 190,
		blankText: '请选择执行人',
		listeners: {
			   'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt.ownerCt.ownerCt.ownerCt;
					cmp.getEl().on('click', function(){self.showUser2();}, self); 
				}
		}
			/*,
		listeners: {
		   'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt.ownerCt.ownerCt.ownerCt;
				cmp.getEl().on('click', self.showSelector, self); 
			}
		}*/
	});
	this.txtTaskOrigin = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '任务来源',
        store: new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.TASKORIGINVALUE
        }),
        value: 0,
        valueField: 'value',
        displayField: 'name',
        editable: false,
        disabled:true,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	
	this.txtContent = new Ext.form.HtmlEditor({
		fieldLabel: '<br><br><br><br><br><br><br>任务内容',
		width:400,
		height:150,
		//readOnly:true,	// Modified By Greco (Fixed IE HtmlEditor Error)
		designMode:'off',//Modified by wilson
		autoScroll:true,
		enableSourceEdit: false
	});
	this.dtFeedback = new Ext.ux.form.DateTime({
		fieldLabel: '反馈时间',
		timeFormat:'H:i',
        timeConfig: {
             altFormats:'H:i',
            allowBlank: true,
              disabled:true   
        },
        dateFormat:'Y-m-d',
        dateConfig: {
            altFormats:'Y-m-d',
            allowBlank: false,   
              disabled:true 
       }
	});
	this.chkRemind = new Ext.form.CheckboxGroup({
		width:110,
		fieldLabel: '提醒设置',
		items: [
		        {boxLabel: '短信', name: 'smsRemind'},
		        {boxLabel: '邮件', name: 'emailRemind'}
		]
	});
	this.taskPercent = new Ext.form.NumberField({
		fieldLabel:'完成百分比',
		allowBlank:false,
		maxValue:100,
		minValue:0
		
	});
	this.txtCompleteStatus = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '完成状态',
        store: new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: [['1', '未完成'], ['3', '完成'],['4', '已中止']]
        }),
        valueField: 'value',
        displayField: 'name',
        editable: false,
        allowBlank:false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.feedBackContent = new Ext.form.TextArea({
		fieldLabel:'完成情况',
		width:400,
		height:50,
		maxLength:250
	});
	this.feedBackFile = new Ext.form.FileUploadField({
		fieldLabel : '反馈附件',
		emptyText: '请选择要上传的文件',
		width : 400,
		name : 'feedBackFileName',
		buttonText: '',
		buttonCfg: {
            iconCls: 'icon-compress'
        }
	});
	this.form = new Ext.FormPanel({
		title:'任务信息',
        bodyStyle: 'padding: 5px 5px 0',
        labelWidth: 75,
        frame:true,
        fileUpload: true, 
        labelAlign: 'right',
        width: 500,
        autoScroll: true,
        items: [this.txtTitle, this.txtContent, {
        	layout: 'column',
        	defaults: {layout: 'form'},
        	items: [ {
        			columnWidth: .6,
        			items: [this.txtExecutor, this.dtFeedback, this.txtCompleteStatus]
        		},{
        			columnWidth: .4,
        			items: [this.txtTaskOrigin, this.chkRemind]
        		}
        	]
        }, this.feedBackContent,this.feedBackFile]
    });
	this.addCon = new Ext.data.Connection({
		url:'../task/feedBackTask.form?timesnap=' + Date.parse(new Date()),
		method:'post'
	});
	this.zfTask = new Ext.data.Connection({
		url:'../task/zfTask.form?timesnap=' + Date.parse(new Date()),
		method:'post'
	});
	this.btnBack = new Ext.Button({
		 text:'反馈',
         scope: this,
         handler:this.feedBack
	});
	this.btnChange = new Ext.Button({
		 text:'转派',
         scope: this,
         handler:this.change
	});
	this.tabPanel  = new Ext.TabPanel({
		height:350
	});
	this.tabPanel.add(this.form);
	this.tabPanel.activate(this.form);
	this.feedHistoryPanel = new Task.FeedBack.History.panel();
	this.tabPanel.add(this.feedHistoryPanel);
	this.sendHistoryPanel = new Task.Send.History.panel();
	this.tabPanel.add(this.sendHistoryPanel);
	this.taskDownLoadPanel = new Task.DownLoad.panel();
	this.tabPanel.add(this.taskDownLoadPanel);
//	if(!Task.FeedBack.History.panel.instance)
//		Task.FeedBack.History.panel.instance = new Task.FeedBack.History.panel();
//	this.tabPanel.add(Task.FeedBack.History.panel.instance);
//	if(!Task.Send.History.panel.instance)
//		Task.Send.History.panel.instance = new Task.Send.History.panel();
//	this.tabPanel.add(Task.Send.History.panel.instance);
	
	Task.FeedBack.win.superclass.constructor.call(this, {
		title: '任务派发',
        bodyStyle: 'padding: 5px 5px 0',
        autoHeight: true,
        width: 800,
        resizable: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.tabPanel],
        buttons: [this.btnBack,this.btnChange,new Ext.Button({text:'关闭',scope:this,handler:function(){this.hide()}})],
        buttonAlign:'center'
	});
};
Ext.extend(Task.FeedBack.win, Ext.Window, {
	initFeedBack:function(data){
		this.setTitle("反馈任务");
		this.form.getForm().reset();
		this.taskId = data.TASK_ID;
		this.taskExeId = data.TASK_EXE_ID;
		this.taskPercent.setValue(data.TASK_EXE_PERCENT)
		this.txtTitle.setValue(data.TASK_TITLE);
		this.txtContent.setValue(data.TASK_CONTENT);
		this.dtFeedback.setValue(data.TASK_COMPLETE_DATE);
		this.txtTaskOrigin.setValue(data.TASK_ORIGIN);
		this.to = data.TASK_EXE_DISPATCHER_ID;
		this.txtExecutor.disable();
		this.feedHistoryPanel.initFeedBack(this.taskExeId);
		this.sendHistoryPanel.initFeedBack(this.taskExeId);
		this.taskDownLoadPanel.initFeedBack(data.TASK_ID);
		this.taskDownLoadPanel.queryUpfile(data.TASK_ID);
		this.btnBack.show();
		this.btnChange.hide();
		
		this.txtTitle.setDisabled(true);
		this.feedBackContent.setDisabled(false);
		this.txtCompleteStatus.setValue();
		this.txtCompleteStatus.setDisabled(false);
		this.txtContent.setDisabled(true);
		this.feedBackFile.setDisabled(false);
		
		var conn = new Ext.data.Connection({
			url:'../task/searchTaskExes.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		});
		Utils.request(conn,{
			params:{
				taskId:this.taskId
			},
			scope:this,
			success:function(data){
				if (!this.scope.userSelector) {
					this.scope.userSelector = new Selector.User(this.scope, '执行人');
				}
				var userNames = [];
				for(var i =0;i<data.exes.length;i++){
					userNames.push(data.exes[i].USER_NAME);
				}
				this.scope.txtExecutor.setValue(userNames.join(';'));
			}
		});
		this.tabPanel.activate(this.form);
	},
	initChange:function(data,backChange){
		this.setTitle("转派任务");
		this.form.getForm().reset();
		this.taskId = data.TASK_ID;
		this.taskExeId = data.TASK_EXE_ID;
		this.txtTitle.setValue(data.TASK_TITLE);
		this.taskPercent.setValue(data.TASK_EXE_PERCENT);
		this.txtContent.setValue(data.TASK_CONTENT);
		this.dtFeedback.setValue(data.TASK_COMPLETE_DATE);
		this.txtTaskOrigin.setValue(data.TASK_ORIGIN);
		this.taskExeStatus = data.TASK_EXE_STATUS;
		this.feedBackContent.fieldLabel="转派原因";
		if(backChange&& backChange=="backChange"){//退回转发人派发人
			this.taskSender = data.TASK_EXE_DISPATCHER_ID;
		}else{
			this.taskSender = data.TASK_EXE_USER_ID;
		}
		this.taskExecutor = data.TASK_EXE_DISPATCHER_ID;
		this.txtCompleteStatus.setValue(1);
		this.txtCompleteStatus.disable();
		this.txtExecutor.enable();
		this.feedHistoryPanel.initFeedBack(this.taskExeId);
		this.sendHistoryPanel.initFeedBack(this.taskExeId);
		this.taskDownLoadPanel.initFeedBack(data.TASK_ID);
		this.taskDownLoadPanel.queryUpfile(data.TASK_ID);
		this.btnBack.hide();
		this.btnChange.show();
		this.tabPanel.activate(this.form);
	},
	initDetail:function(data){
		this.form.getForm().reset();
		this.taskId = data.TASK_ID;
		this.taskExeId = data.TASK_EXE_ID;
		if(data.TASK_EXE_PERCENT==undefined)
			data.TASK_EXE_PERCENT=0;
		this.taskPercent.setValue(data.TASK_EXE_PERCENT);
		this.txtTitle.setValue(data.TASK_TITLE);
		this.txtContent.setValue(data.TASK_CONTENT);
		this.dtFeedback.setValue(data.TASK_COMPLETE_DATE);
		this.txtTaskOrigin.setValue(data.TASK_ORIGIN);
		this.txtExecutor.disable();
		this.feedHistoryPanel.initFeedBack(this.taskExeId);
		this.sendHistoryPanel.initFeedBack(this.taskExeId);
		this.sendHistoryPanel.queryTask(this.taskExeId);
		this.taskDownLoadPanel.initFeedBack(data.TASK_ID);
		this.taskDownLoadPanel.queryUpfile(data.TASK_ID);
		this.btnBack.show();
		this.btnChange.hide();
		
		this.txtTitle.setDisabled(true);
		this.feedBackContent.setValue(data.TASK_EXE_STATE);
		this.feedBackContent.setDisabled(true);
		this.txtCompleteStatus.setValue(1);
		this.txtCompleteStatus.setDisabled(true);
		this.txtContent.setDisabled(true);
		this.feedBackFile.setDisabled(true);
		this.chkRemind.setDisabled(true);
		
		var conn = new Ext.data.Connection({
			url:'../task/searchTaskExes.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		});
		Utils.request(conn,{
			params:{
				taskId:this.taskId
			},
			scope:this,
			success:function(data){
				if (!this.scope.userSelector) {
					this.scope.userSelector = new Selector.User(this.scope, '执行人');
				}
				var userNames = [];
				for(var i =0;i<data.exes.length;i++){
					userNames.push(data.exes[i].USER_NAME);
				}
				this.scope.txtExecutor.setValue(userNames.join(';'));
			}
		});
		this.btnBack.hide();
		this.tabPanel.activate(this.form);
	},
	feedBack: function(btn, event) {
		if(!this.form.getForm().isValid()){
			return;
		}
		if(this.feedBackContent.getValue() ==""){
			Utils.info("反馈内容为必填项！");
			return;
		}
		//if(this.taskPercent.getValue()!=100&&this.txtCompleteStatus.getValue()=='3'){
		//	Utils.info("完成百分比应该为100");
		//	return;
		//}
		if (!this.loadMask) {
			 this.loadMask = new Ext.LoadMask(this.form.getEl(), {msg: '派发中，请稍候...'});
		}
		scope=this,
		this.form.getForm().submit({
			waitTitle : '系统提示',
			waitMsg : '数据提交中，请稍候...',
			url:'../task/feedBackTask.form?timesnap=' + Date.parse(new Date()),
			method:'post',
		//Utils.request(this.addCon,{
			params:{//反馈参数
				content : this.feedBackContent.getValue(),
				fb_status : this.txtCompleteStatus.getValue() ,
				exeId : this.taskExeId,
				taskId:this.taskId,
				to:this.to,//反馈接收人
				percent : this.taskPercent.getValue(),
				back_status : '1',
				fileName:this.feedBackFile.getValue(),
				fileRemark:'反馈附件',
				taskOrigin:this.txtTaskOrigin.getValue()
			},
			//scope:this,
			success:function(){
				Utils.info("操作成功！");
				scope.hide();
				if(Task.Open.panel.instance)
					Task.Open.panel.instance.store.reload();
				if(Task.Close.panel.instance)
					Task.Close.panel.instance.store.reload();
			}
		});
	},
	change:function(){
		if(!this.form.getForm().isValid()){
			return;
		}
		if(this.feedBackContent.getValue() ==""){
			Utils.info("转派原因为必填项！");
			return;
		}
		if(this.txtExecutor.getValue() ==""){
			Utils.info("请指定转派人！");
			return;
		}
		Utils.request(this.zfTask,{
			params:{//转派参数
				taskId : this.taskId,
				task_status :this.taskExeStatus,
				exeId : this.taskExeId,
				percent : this.taskPercent.getValue(),
				remark :this.feedBackContent.getValue() ,
				from :this.taskSender ,
				to : this.selectUserId2
			},scope:this,
			success:function(){
				Utils.info("操作成功！");
				this.scope.hide();
				if(!(Task.Open===undefined))
					Task.Prepare.panel.instance.store.reload();
				if(!(Task.Back===undefined))
					Task.Prepare.panel.instance.store.reload();
			}
		});
	},
/*	showSelector: function(event) {
		if (!this.userSelector) {
			this.userSelector = new Selector.User(this, '执行人');
		}
		this.userSelector.initUsers(this.users);
		this.userSelector.show();
	},
	setUsers: function(o) {	// for Selector.User callback
		this.users = o;
		var names = [];
		for(var i=0, u; u = this.users[i++];) {
			names.push(u.name);
			this.selectUserId.push(u.id);
		}
		this.txtExecutor.setValue(names.join(';'));
	},*/
	
	 showUser2: function(){
		var scope=this;
		if(this.userSelector2==null){
			this.userSelector2=new Tree.User(this.selectUserId2,function(o){
				scope.setUsers2(o);
			},null,'101');
		}
		this.userSelector2.show();
	},
	 setUsers2: function(o){
		var names = [];
		this.selectUserId2 =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			this.selectUserId2.push(o[i].id);
		}
		this.txtExecutor.setValue(names.join(';'));
	}
});

